<?php
namespace app\admin\controller;
use app\admin\model\Position as PositionModel;
use app\admin\controller\Base;

class Category extends Base
{
    
    public function lst()
    {
        $list=db('category')->select();
        $this->assign('list',$list);


        return view();
    }

    public function add()
    {
        if (request()->isPost())
        {
            $data=[
                'name'=>input('name'),
                'level'=>input('level')
            ];
            $validate = \think\Loader::validate('Position');
            if (!$validate->scene('add')->check($data))
            {
                $this->error($validate->getError());die;
            }
            if (db('position')->insert($data))
        {
            return $this->success('部门添加成功','lst');
        }
        else
        {
            return $this->error('部门添加失败','lst');
        }

            return ;
        }
        return $this->fetch();
    }
    
    public function edit()
    {
        $id=input('id');
        $position=db('position')->find($id);
        if (request()->isPost())
        {
            $data=[
                'name'=>input('name'),
                'level'=>input('level')
            ];

            $validate = \think\Loader::validate('Position');
            if (!$validate->scene('edit')->check($data))
            {
                $this->error($validate->getError());die;
            }
            $save=db('position')->where('id','=',$id)->update($data);
            if ($save!==false)
            {
                $this->success('修改部门成功','lst');
            }
            else {
                $this->error('修改部门失败');
            }
        }
        
        
        $this->assign('position',$position);
        return $this->fetch();
    }
    
    public function del()
    {
        $id=input('id');
            if (db('category')->delete($id)){
                $this->success('类型删除成功','lst');
            }
            else {
                $this->error('类型删除失败','lst');
            }
        }


    public function ajaxadd()
    {
        $input=input();
        $save=db('category')->insert($input);
        if($save){
            return ['ECODE'=>200,'MSG'=>'添加成功'];
        }
    }






}
